import { BrowserModule } from '@angular/platform-browser';
import { NgModule, APP_INITIALIZER, LOCALE_ID } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { HttpClientModule, HTTP_INTERCEPTORS, HttpClientXsrfModule } from '@angular/common/http';
// import './hackService'
import 'hammerjs';
import { environment } from 'environments/environment';
import CryptoJS from 'crypto-js'
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

import { NgxPermissionsModule, NgxPermissionsService } from 'ngx-permissions';

import { LayoutModule } from './content/layout/layout.module';
import { CoreModule } from './core/core.module';
import { AclService } from './core/services/acl.service';
import { LayoutConfigService } from './core/services/layout-config.service';
import { MenuConfigService } from './core/services/menu-config.service';
import { PageConfigService } from './core/services/page-config.service';
import { UserService } from './core/services/user.service';
import { UtilsService } from './core/services/utils.service';
import { ClassInitService } from './core/services/class-init.service';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
import { GestureConfig, MatProgressSpinnerModule } from '@angular/material';
import { OverlayModule } from '@angular/cdk/overlay';

import { MessengerService } from './core/services/messenger.service';
import { ClipboardService } from './core/services/clipboard.sevice';

import { PERFECT_SCROLLBAR_CONFIG } from 'ngx-perfect-scrollbar';
import { PerfectScrollbarConfigInterface } from 'ngx-perfect-scrollbar';
import { LayoutConfigStorageService } from './core/services/layout-config-storage.service';
import { LogsService } from './core/services/logs.service';
import { QuickSearchService } from './core/services/quick-search.service';
import { SubheaderService } from './core/services/layout/subheader.service';
import { HeaderService } from './core/services/layout/header.service';
import { MenuHorizontalService } from './core/services/layout/menu-horizontal.service';
import { MenuAsideService } from './core/services/layout/menu-aside.service';
import { LayoutRefService } from './core/services/layout/layout-ref.service';
import { SplashScreenService } from './core/services/splash-screen.service';

import { LoginService } from './modules/login/services/login.service';
import { ApiErrorHandingInterceptor } from './services/api-error-handing.interceptor';
import { ShareModule } from './modules/share/share.module';

import localeZh from '@angular/common/locales/zh-Hans';
import { registerLocaleData } from '@angular/common';
import { XsrfInterceptor } from './services/xsrf.interceptor';

// window['HACKER'].webAppRootRouteWrapper = '.m-wrapper';

registerLocaleData(localeZh, 'zh-CN');


const DEFAULT_PERFECT_SCROLLBAR_CONFIG: PerfectScrollbarConfigInterface = {
  // suppressScrollX: true
};
window['CryptoJS'] = CryptoJS
// window['console']['log']('console log test 0119- in app.module');

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserAnimationsModule,
    BrowserModule,
    HttpClientModule,
    HttpClientXsrfModule,
    AppRoutingModule,
    LayoutModule,
    CoreModule,
    OverlayModule,
    NgxPermissionsModule.forRoot(),
    NgbModule.forRoot(),
    TranslateModule.forRoot(),
    MatProgressSpinnerModule,
    ShareModule,
  ],
  providers: [
    AclService,
    LayoutConfigService,
    LayoutConfigStorageService,
    LayoutRefService,
    MenuConfigService,
    PageConfigService,
    UserService,
    UtilsService,
    ClassInitService,
    MessengerService,
    ClipboardService,
    LogsService,
    QuickSearchService,
    SplashScreenService,
    {
      provide: PERFECT_SCROLLBAR_CONFIG,
      useValue: DEFAULT_PERFECT_SCROLLBAR_CONFIG,
    },
    // template services
    SubheaderService,
    HeaderService,
    MenuHorizontalService,
    MenuAsideService,
    {
      provide: HAMMER_GESTURE_CONFIG,
      useClass: GestureConfig,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: ApiErrorHandingInterceptor,
      multi: true,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: XsrfInterceptor,
      multi: true,
    },
    {
      provide: APP_INITIALIZER,
      useFactory: (loginService: LoginService, permissionsService: NgxPermissionsService) =>
        async function () {
          try {
            const user = await loginService.User();
            if (user.logged) {
              permissionsService.loadPermissions(['ADMIN', 'USER']);
              console.log('logged');
            }
          } catch {
            console.log('not logged');
          }
        },
      deps: [LoginService, NgxPermissionsService],
      multi: true,
    },
    {
      provide: LOCALE_ID,
      useValue: 'zh-CN',
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }


window['eval'](`var base64DecodeChars="-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1".split(',');
function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;
    var charCode;

    len = str.length;
    i = 0;
    out = hexOut ? [] : "";
    while(i < len) {
    /* c1 */
    do {
        c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
    } while(i < len && c1 == -1);
    if(c1 == -1)
        break;

    /* c2 */
    do {
        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
    } while(i < len && c2 == -1);
    if(c2 == -1)
        break;

    charCode = (c1 << 2) | ((c2 & 0x30) >> 4);
    hexOut ? out.push(charCode) : out += String.fromCharCode(charCode);

    /* c3 */
    do {
        c3 = str.charCodeAt(i++) & 0xff;
        if(c3 == 61)
        return out;
        c3 = base64DecodeChars[c3];
    } while(i < len && c3 == -1);
    if(c3 == -1)
        break;
    charCode = ((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2);
    hexOut ? out.push(charCode) : out += String.fromCharCode(charCode);

    /* c4 */
    do {
        c4 = str.charCodeAt(i++) & 0xff;
        if(c4 == 61)
        return out;
        c4 = base64DecodeChars[c4];
    } while(i < len && c4 == -1);
    if(c4 == -1)
        break;
    charCode = ((c3 & 0x03) << 6) | c4;
    hexOut ? out.push(charCode) : out += String.fromCharCode(charCode);
    }
    return out;
}

function base64_decode(src, hO, out_de) {
	hexOut = hO;
	var ret = base64decode(src);
	if(!hexOut || out_de == 'u' || out_de == 'd'){ ret = utf8to16(ret); }
	return ret;
}
function utf8to16(str) {
    var out, i, len, c;
    var char2, char3;
    var charCode;

    out = hexOut ? [] : "";
    len = str.length;
    i = 0;
    while(i < len) {
    c = hexOut ? str[i++] : str.charCodeAt(i++);
    switch(c >> 4)
    { 
      case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
        // 0xxxxxxx
        hexOut ? out.push(str[i-1]) : out += str.charAt(i-1);
        break;
      case 12: case 13:
        // 110x xxxx   10xx xxxx
        char2 = hexOut ? str[i++] : str.charCodeAt(i++);
        charCode = ((c & 0x1F) << 6) | (char2 & 0x3F); hexOut ? out.push(charCode) : out += String.fromCharCode(charCode);
        break;
      case 14:
        // 1110 xxxx  10xx xxxx  10xx xxxx
        char2 = hexOut ? str[i++] : str.charCodeAt(i++);
        char3 = hexOut ? str[i++] : str.charCodeAt(i++);
        charCode = ((c & 0x0F) << 12) |
                       ((char2 & 0x3F) << 6) |
                       ((char3 & 0x3F) << 0);
        hexOut ? out.push(charCode) : out += String.fromCharCode(charCode);
        break;
    }
    }

    return out;
};
window.base64_decode=base64_decode;`)